!function () {
    let token = '';
    // cookie 的 增添改查 对象
    const CookieUtil = {
        // 获取cookie
        get: function (name) {
            var cookieName = encodeURIComponent(name) + "=",
                cookieStart = document.cookie.indexOf(cookieName),
                cookieValue = null;
            if (cookieStart > -1) {
                var cookieEnd = document.cookie.indexOf(":", cookieStart);
                if (cookieEnd === -1) {
                    cookieEnd = document.cookie.length;
                }
                cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length, cookieEnd));
            }
            return cookieValue;
        },
        // 设置创建 写入 cookie
        set: function (name, value, expires, path, domain, secure) {
            var cookieText = encodeURIComponent(name) + "=" + encodeURIComponent(value);
            if (expires instanceof Date) {
                cookieText += "; expires =" + expires.toGMTString();
            }
            if (path) {
                cookieText += "; path =" + path;
            }
            if (domain) {
                cookieText += "; secure;"
            }
            document.cookie = cookieText;
        },
        // 删除cookie
        unset: function (name, path, domain, secure) {
            this.set(name, "", new Date(0), path, domain, secure);
        }
    };
    const main = {
        // 查看是否有Session中是否有id 和 password
        lookOfSession: function () {
            // token = sessionStorage.getItem('Token');
            token = CookieUtil.get('Token');
        },
        // 如果 账号和密码存在则获取用户名称
        hasSession: function () {
            if (token !== null) {
                $.ajax({
                    url: 'https://www.easy-mock.com/mock/5ad9997a9920725e3e3e3146/' + token,
                    type: 'get',
                    success: function (s) {
                        main.getUserMessage(s);
                    },
                    error: function (e) {
                        console.log('有session时请求' + e);
                    },
                });
            }
        },
        // 获取 用户人信息
        getUserMessage: function (s) {
            console.log(s);
            let userName = s.data.name;
            let ImageURL = s.data.imageURL;
            console.log(userName + ':' + ImageURL);
            main.drawDOM(userName, ImageURL);
        },
        // 渲染 DOM
        drawDOM: function (userName, ImageURL) {
            $('.login-text').text(userName);
            $('.login-head-icon').attr('src', ImageURL);

        },
        // 主干
        master: function () {
            // 查看是否有 token
            main.lookOfSession();
            // 如果有token加载 token对应数据
            main.hasSession();
        }
    };
    main.master();
}();